﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Tenebrous_Manor.SceneManager
{
    class EndingCutScene
    {
        bool initialize;
        bool see_shades;
        bool scream1;
        bool scream2;
        bool draw_main_entrance;
        bool main_entrance;
        bool end1;
        bool end2;

        float elapsed = 0.0f;

        public Texture2D shade;

        KeyboardState oldState;
        KeyboardState newState;

        Rectangle shade_rect = new Rectangle(0, 220, 85, 110);
        Rectangle gary_rect = new Rectangle(170, 220, 85, 110);

        Vector2[] shadePosition = new Vector2[4];

        public void Initialize(Texture2D shade)
        {
            this.shade = shade;

            initialize = false;
            see_shades = false;
            scream1 = false;
            scream2 = false;
            draw_main_entrance = false;
            main_entrance = false;
            end1 = false;
            end2 = false;

            elapsed = 0.0f;

            shadePosition[0] = new Vector2(5 * 85, 5 * 85);
            shadePosition[1] = new Vector2(4 * 85, 2 * 85);
            shadePosition[2] = new Vector2(2 * 85, 4 * 85);
            shadePosition[3] = new Vector2(5 * 85, 7 * 85);
        }

        public bool Update(GameTime gameTime, ref Player player, ref TextBox textBox)
        {
            if(!end1)
                player.spriteMove(gameTime);

            if (!initialize)
            {
                player.setLight(false);
                player.setPosition(RandomizedClueLocations.breaker.Item1 - 1, RandomizedClueLocations.breaker.Item2);
                player.setFacing(Facing.RIGHT);

                elapsed += gameTime.ElapsedGameTime.Milliseconds;

                if (elapsed >= 2000)
                {
                    elapsed = 0.0f;
                    initialize = true;
                    textBox.setString(Dialogue.dialogue[Dialogue.Dialogue_keys.SEE_SHADES]);
                    textBox.triggered = true;
                }
            }
            else if (!see_shades)
            {
                player.setFacing(Facing.LEFT);

                if (player.getLocation().X <= (8.25 * 85))
                    player.incrementLocation(new Vector2(player.getVelocity() / 5, 0));

                if (!textBox.triggered && textBox.done_printing)
                {
                    see_shades = true;
                    textBox.setString(Dialogue.dialogue[Dialogue.Dialogue_keys.SCREAM1]);
                    textBox.triggered = true;
                }
            }
            else if (!scream1)
            {
                if (shadePosition[0].X <= (7 * 85))
                {
                    shadePosition[0].X += 1.5f;
                }

                if (shadePosition[1].X <= (7 * 85))
                {
                    shadePosition[1].X += 1.5f;
                }

                if (shadePosition[2].X <= (7 * 85))
                {
                    shadePosition[2].X += 1.5f;
                }

                if (shadePosition[3].X <= (7 * 85))
                {
                    shadePosition[3].X += 1.5f;
                }

                if (!textBox.triggered && textBox.done_printing)
                {
                    scream1 = true;
                    textBox.setString(Dialogue.dialogue[Dialogue.Dialogue_keys.SCREAM2]);
                    textBox.triggered = true;
                }
            }
            else if (!scream2)
            {
                if (!textBox.triggered && textBox.done_printing)
                {
                    scream2 = true;
                    draw_main_entrance = true;
                    player.WalkInCutScene(true, Facing.DOWN, 5, 3);
                }
            }
            else if (!main_entrance)
            {
                if (player.getLocation().Y <= 6 * 85)
                {
                    player.incrementLocation(new Vector2(0, player.getVelocity()));
                }
                else
                {
                    elapsed = 0.0f;
                    main_entrance = true;
                    player.WalkInCutScene(false, Facing.DOWN, 5, 6);
                    player.setLight(false);
                    textBox.setString(Dialogue.dialogue[Dialogue.Dialogue_keys.END1]);
                }
            }
            else if (!end1)
            {
                elapsed += gameTime.ElapsedGameTime.Milliseconds;

                if (elapsed >= 1000 && elapsed <= 1250)
                {
                    textBox.triggered = true;
                }

                player.EndCutScene(true, 1);

                if (!textBox.triggered && textBox.done_printing)
                {
                    end1 = true;
                    textBox.setString(Dialogue.dialogue[Dialogue.Dialogue_keys.END2]);
                    elapsed = 0.0f;
                }
            }
            else if (!end2)
            {
                player.EndCutScene(true, 2);

                if(Randomization.RandomResearchNotes.foundAllNotes())
                {
                    elapsed += gameTime.ElapsedGameTime.Milliseconds;

                    if (elapsed >= 1000 && elapsed <= 1250)
                    {
                        textBox.triggered = true;
                    }

                    if (!textBox.triggered && textBox.done_printing)
                    {
                        end2 = true;
                    }
                }
                else
                {
                    end2 = true;
                }
            }
            else
            {
                player.EndCutScene(false, 1);
                return true;
            }

            newState = Keyboard.GetState();
            if (textBox.triggered)
            {
                if (newState.IsKeyDown(Keys.Enter) && !oldState.IsKeyDown(Keys.Enter))
                {
                    if (textBox.done_printing)
                        textBox.triggered = !textBox.triggered;

                    else
                        textBox.continued = true;

                }
            }
            textBox.Update(gameTime, ref player.position);
            oldState = newState;

            return false;
        }

        public void Draw(SpriteBatch spriteBatch, SpriteFont font, ref Player player, ref TextBox textBox)
        {
            if (!scream1)
            {
                Navigation.RoomGraphics.Draw(spriteBatch, Navigation.Room.RESEARCH_LAB);

                spriteBatch.Draw(shade, shadePosition[0], gary_rect, Color.White);
                spriteBatch.Draw(shade, shadePosition[1], shade_rect, Color.White);
                spriteBatch.Draw(shade, shadePosition[2], shade_rect, Color.White);
                spriteBatch.Draw(shade, shadePosition[3], shade_rect, Color.White);
                player.Draw(spriteBatch);
            }

            if (draw_main_entrance)
            {
                Navigation.RoomGraphics.Draw(spriteBatch, Navigation.Room.MAIN_ENTRANCE);
                player.Draw(spriteBatch);
            }

            textBox.Draw(spriteBatch);
        }
    }
}
